package com.aotain.project.apollo.bolt;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class DosStatusSliding implements Serializable {

	/** 
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) 
	 */ 
	private static final long serialVersionUID = 1L;

	private Map<Integer,HashMap<String,Double>> tupMap = new HashMap<Integer,HashMap<String,Double>>();//熵
	private int headSlot;
	private int tailSlot;
	private int slotNum;
	
	public DosStatusSliding(int slotNum) {
		this.slotNum = slotNum;
		if(slotNum < 2) {
			System.err.	println("滑动窗口至少要有两个");
		}
		
		for(int i=0;i<slotNum;i++) {
			tupMap.put(i, null);
		}
		headSlot = 0;
		tailSlot = (headSlot + 1) % this.slotNum;
	}
	
	//添加消息元素(熵)
	public void addPInfo(HashMap<String,Double> map) {
		HashMap<String,Double> m = new HashMap<String,Double>();
		m.putAll(map);
		tupMap.put(headSlot, m);
		m = null;
	}
	
	
	//获取当前所有窗口中关于信息熵的内容
	public HashMap<Integer, HashMap<String, Double>> getTuplesWindow() {
		int i = headSlot;
		HashMap<Integer,HashMap<String,Double>> winTuples = new HashMap<Integer,HashMap<String,Double>>();
		if(tupMap.get(i) != null) {
			winTuples.put(i,tupMap.get(i));			
		}
		
		while((i = slotAfter(i)) != headSlot) {
			winTuples.put(i,tupMap.get(i));
		}
		return winTuples;
	}
	

	//向前移动窗口
	public void advanceHead() {
		headSlot = tailSlot;
		wipeSlot(headSlot);
		tailSlot = slotAfter(tailSlot);
	}
	
	//清空
	public void wipeSlot(int slot) {
		tupMap.put(slot, null);
	}
	
	//向下移动一个
	public int slotAfter(int slot) {
		return (slot + 1) % slotNum;
	}
}
